package ocg.common.re.feed;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * Created by chenbo on 2018/3/7.
 * 对 键值对 数据集进行 处理
 */
public class Feed {

    private List<Map<String,Object>> feeder;

    public Feed(List <Map <String, Object>> feed) {
        this.feeder = feed;
    }

    /**
     * 通过 列名 row 筛选，返回 list
     * @param row       列名
     * @return
     */
    public List<Object> feeder( String row){
        List<Object> list = new ArrayList <Object> (  );
        if ( feeder != null ) {
            for (int i = 0; i < feeder.size (); i++) {
                Map <String, Object> m = feeder.get ( i );
                list.add ( m.get ( row ) );
            }
        }
        return list;
    }

    /**
     * 通过 index 返回一条数据 map
     * @param index
     * @return
     */
    public Map<String,Object> feeder( int index ){
        Map<String,Object> map = new HashMap <String, Object> ( );
        if ( feeder != null ) {
            if (index < size ()) {
                map = feeder.get ( index );
            }
        }
        return map;
    }

    /**
     * 数据集大小
     * @return
     */
    public int size(){
        int size = 0 ;
        if ( feeder != null ){
            size = feeder.size ();
        }
        return size;
    }

    public Object random( String row ){

        Random random = new Random ( );
        int inf = random.nextInt ( feeder.size () );
        return feeder.get ( inf ).get ( row );
    }
}
